//
//  PrintUtil.c
//  AlgorithmLearning
//
//  Created by aron on 2017/11/3.
//  Copyright © 2017年 aron. All rights reserved.
//

#include "PrintUtil.h"
#include <math.h>

#pragma mark - Print Helper

int printArr(ElementType arr[], int count, _Bool (*ruleFunPtr)(ElementType a, ElementType b)) {
    _Bool isValid = 1;
    ElementType lstItem = INT16_MAX;
    for (int i = 0; i < count; i++) {
        printf("%d ", arr[i]);
        if (lstItem != INT16_MAX) {
            if (!ruleFunPtr(lstItem, arr[i])) {
                isValid = 0;
            }
        }
        
        lstItem = arr[i];
    }
    if (isValid) {
        printf("\nValid\n");
    } else {
        printf("\nInValid\n");
    }
    return 1;
}

int printAscArr(ElementType arr[], int count) {
    printArr(arr, count, ascRule);
    return 1;
}

int printDescArr(ElementType arr[], int count) {
    printArr(arr, count, descRule);
    return 1;
}

_Bool ascRule(ElementType a, ElementType b) {
    return a <= b;
}

_Bool descRule(ElementType a, ElementType b) {
    return a >= b;
}

void printPQStructure(PriorityQueue H) {
    float rowss = logf(H->Size + 1) / logf(2);
    int rows = ceilf(rowss);
    printf("rows = %d \n\n", rows);
    printf("\n=====================================\n");
    int index = 1;
    for (int i = 0; i<rows; i++) {
        if (index > H->Size) {
            break;
        }
        int rowCount = powf(2, i);
        int startX = powf(2, rows - (1 + i)) - 1;
        int margin = powf(2, rows - i) - 1;
        int j;
        int startBlankIndex;
        for (startBlankIndex = 0; startBlankIndex<startX; startBlankIndex++) {
            printf("  ");
        }
        for (j = 0; j<rowCount; j++) {
            printf("%2d", H->Elements[index]);
            int marginBlankIndex;
            for (marginBlankIndex = 0; marginBlankIndex<margin; marginBlankIndex++) {
                printf("  ");
            }
            index ++;
            if (index > H->Size) {
                break;
            }
        }
        
        printf("\n");
    }
    printf("\n=====================================");
    printf("\n");
}
